﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Raise3000.Code.Foundation;

namespace Raise3000.Code.Attribute_Calculator
{
    // The purpose this class is to encapsulates all logic which is related to the hand context.
    public class HandContextHelper
    {
        private HandContext handContext_ = null;

        public HandContextHelper(HandContext handContext)
        {
            handContext_ = handContext;
        }

        public void UpdateHandContext(PlayerAction lastAction)
        {
            // update the current pot
            if (lastAction.Move == ActionEnum.eBET &&
                lastAction.Move == ActionEnum.eCALL &&
                lastAction.Move == ActionEnum.eRAISE)
            {
                handContext_.AddToPot(lastAction.Amount);
            }
            // add the last action to the privious actions list.
            switch (handContext_.Street)
            {
                case StreetEnum.ePREFLOP:
                    {
                        handContext_.AddToPreflop(lastAction);
                        break;
                    }
                case StreetEnum.eFLOP:
                    {
                        handContext_.AddToFlop(lastAction);
                        break;
                    }
                case StreetEnum.eTURN:
                    {
                        handContext_.AddToTurn(lastAction);
                        break;
                    }
                case StreetEnum.eRIVER:
                    {
                        handContext_.AddToRiver(lastAction);
                        break;
                    }
            };
            
        }
                 
        public StreetEnum GetCurrentStreet()
        {
            return handContext_.Street;
        }

        public List<string> GetCurrentPlayers()
        {
            return handContext_.GetCurrentPlayers();
        }

        public bool IsPreflop()
        {
            return handContext_.Street == StreetEnum.ePREFLOP;
        }

        public bool IsFlop()
        {
            return handContext_.Street == StreetEnum.eFLOP;
        }

        public bool IsTurn()
        {
            return handContext_.Street == StreetEnum.eTURN;
        }

        public bool IsRiver()
        {
            return handContext_.Street == StreetEnum.eRIVER;
        }

        // on the current street test whether is bet behind, and no raise after.
        // use this method in flop, turn and river streets, makes no sense using it in preflop.
        public bool IsThereBetBehind()
        {
            bool isBetBehind = false;

            List<PlayerAction> actions = handContext_.GetActions(handContext_.Street);

            int bets = 0;
            int raises = 0;
            for (int idx = 0; idx < actions.Count; ++idx)
            {
                if (actions[idx].Move == ActionEnum.eBET)
                {
                    bets++;
                }
                else if (actions[idx].Move == ActionEnum.eRAISE)
                {
                    raises++;
                }
            }

            isBetBehind = (bets == 1) && (raises == 0);

            return isBetBehind;
        }

        // on the current street test whether is raise begind and no re-raise(3bet) after.
        public bool IsThereRaiseBehind()
        {
            bool isRaiseBehind = false;

            List<PlayerAction> actions = handContext_.GetActions(handContext_.Street);

            int bets = 0;
            int raises = 0;
            for (int idx = 0; idx < actions.Count; ++idx)
            {
                if (actions[idx].Move == ActionEnum.eBET)
                {
                    bets++;
                }
                else if (actions[idx].Move == ActionEnum.eRAISE)
                {
                    raises++;
                }
            }

            // there is no bet on the preflop
            isRaiseBehind = (raises == 1);

            return isRaiseBehind;
        }
    }
}
